package cn.tedu.charging.order.job;

import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.order.constant.Constant;
import cn.tedu.charging.order.dao.repository.impl.OrderRepositoryImpl;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.UUID;

@Slf4j
public class DeviceCheckJob implements Job {

    public DeviceCheckJob() {
    }

    public DeviceCheckJob(String orderNo,Integer gunId) throws Exception {
        log.debug("添加设备自检定时任务:订单号-{},枪id-{}",orderNo,gunId);
        //创建调度器工厂
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        //通过调度器工厂获取调度器
        Scheduler scheduler = schedulerFactory.getScheduler();

        //创建定时任务
        JobDetail jobDetail = JobBuilder.newJob(DeviceCheckJob.class)
                //定义定时任务的名称和组
                .withIdentity("DeviceCheckJob" + UUID.randomUUID(),"device_check_job")
                //传入订单号
                .usingJobData("orderNo",orderNo)
                //插入枪id
                .usingJobData("gunId",gunId)
                .build();

        //设备自检需要1分钟
        long deviceCheckTime = 1 * 60 * 1000;
        //当前时间
        long now = System.currentTimeMillis();
        //任务触发的时间
        Date triggerTime = new Date(now + deviceCheckTime);
        //创建触发器 并设置时间
        Trigger trigger = TriggerBuilder.newTrigger().startAt(triggerTime).build();

        //调度器 把 任务和触发器  绑定
        scheduler.scheduleJob(jobDetail,trigger);
        //启动调度器
        scheduler.start();
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.debug("设备自检任务开始执行:{}",context);
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        String orderNo = jobDataMap.getString("orderNo");
        int gunId = jobDataMap.getInt("gunId");

        OrderRepositoryImpl orderRepository = SpringContextJobUtils.getBean("orderRepository", OrderRepositoryImpl.class);
        DeviceClient deviceClient = SpringContextJobUtils.getBeanOfType(DeviceClient.class);
        ChargingBillSuccessPO dbChargingBillSuccessPO=orderRepository.getSuccessOrder(orderNo);
        if (dbChargingBillSuccessPO == null) {
            ChargingBillFailPO dbChargingFailPo=orderRepository.getFailOrder(orderNo);
            if (dbChargingFailPo == null) {
                //插入失败订单数据
                ChargingBillFailPO chargingBillFailPO=new ChargingBillFailPO();
                chargingBillFailPO.setBillId(orderNo);
                chargingBillFailPO.setFailDesc("设备自检失败-无响应");
                orderRepository.saveFailOrder(chargingBillFailPO);
                log.debug("设备自检失败,保存失败订单记录:{} 成功.",chargingBillFailPO);

                //修改枪状态为故障
                GunStatusUpdateParam gunStatusUpdateParam=new GunStatusUpdateParam();
                gunStatusUpdateParam.setStatus(Constant.GUN_STATS_ERROR);
                gunStatusUpdateParam.setId(gunId);
                deviceClient.updateGunStatus(gunStatusUpdateParam);
            }else {
                log.debug("设备自检失败-设备有响应但是充不了电,这里的逻辑无需修改 设备失败原因 ");
            }
        }else {
            orderRepository.updateOrderDeviceInfo(orderNo,gunId);
            log.debug("更新设备信息到订单表,这里不是必须的作为演示 更新枪id:{}到成功订单表 成功!!!,更新条件:订单号:{}",gunId,orderNo);
        }
    }
}
